
// ignore_for_file: must_be_immutable

import 'package:flutter/material.dart';
import 'package:flutter_basic_library/flutter_basic_library.dart';
import 'package:flutter_basic_library/utils/num_utils.dart';
import 'package:flutter_basic_library/widget/text_icon_button.dart';

typedef OnSelectData = void Function(MultilevelEntry data);

typedef CreateItemWidget = Widget Function(MultilevelEntry data);

abstract class MultilevelEntry<T> {
  int? level;
  String get title;
  List<MultilevelEntry>? get child;
  bool? isExpend;
  T? get data;///需要携带的数据
}

/**
 * by zjh
 * 2022/11/25  16:36
 * desc:多级列表的展示组件  可自定义样式
 */
class MultilevelListWidget extends StatefulWidget{

  List<MultilevelEntry>?  data;

  OnSelectData? onSelectData;

  ///是否默认全部展开  默认收起的
  bool? isAllExpand;

  ///自定义条目的样式
  CreateItemWidget? createItemWidget;

  MultilevelListWidget({Key? key,this.data,this.isAllExpand=false,this.onSelectData,this.createItemWidget}) : super(key: key);

  @override
  State<StatefulWidget> createState() => MultilevelListWidgetState();

}

class MultilevelListWidgetState extends State<MultilevelListWidget>{

  @override
  void initState() {
    super.initState();
  }

  ///设置数据的层级  默认全部为收起
  setLevel(List<MultilevelEntry> list,int level){
    for (var element in list) {
      element.level=level;
      element.isExpend=widget.isAllExpand!;
      if(element.child!=null&&element.child!.isNotEmpty){
        setLevel(element.child!, level+1);
      }
    }
  }

  @override
  void didUpdateWidget(covariant MultilevelListWidget oldWidget) {
    super.didUpdateWidget(oldWidget);
    if(widget.data!=null){
      setLevel(widget.data!,1);
    }
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    if(widget.data!=null){
      setLevel(widget.data!,1);
    }
  }

  @override
  Widget build(BuildContext context) {
    return widget.data == null?const SizedBox():SingleChildScrollView(
      child: ListView.builder( shrinkWrap: true,/*内嵌listView 这个值要设置成true*/
          primary: true,
          physics: const NeverScrollableScrollPhysics(),itemCount:widget.data!.length,itemBuilder: (context,index){
            return createView(widget.data![index]);
          }),
    );
  }

  Widget createView(MultilevelEntry entry){
    if(entry.child==null || entry.isExpend==null || !entry.isExpend!){
      return titleView(entry);
    }else{
      return Column(
        children: [
          titleView(entry),
          ...entry.child!.map((e) => createView(e)).toList(),
        ],
      );
    }
  }

  Widget titleView(MultilevelEntry entry){
    if(widget.createItemWidget!=null){
      return widget.createItemWidget!.call(entry);
    }else{
      return Container(
        padding: EdgeInsets.only(left: NumUtil.multiply(10, entry.level!),right: 15,top: 5,bottom: 5),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            TextIconButton(icon: Icon(entry.isExpend==null||!entry.isExpend!?Icons.arrow_right_sharp:Icons.arrow_drop_down_sharp,color: Colors.black87,size: 20,), text: Text(entry.title,style:const TextStyle(color: Colors.black87,fontWeight: FontWeight.w600,fontSize: 14),),onTap: (){
              if(entry.isExpend!=null){
                entry.isExpend = !entry.isExpend!;
                setState(() {

                });
              }
            },),
            GestureDetector(
              onTap: (){
                widget.onSelectData?.call(entry);
              },
              child: Text('选取',style: TextStyle(color: Colors.blueAccent,fontSize: 15.sp),),
            ),
          ],
        ),
      );
    }
  }

}
